package com.dream.service.user.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.response.ListModel;
import com.dream.service.product.domain.entity.SpuBase;
import com.dream.service.user.api.UserCollectionService;
import com.dream.service.user.dao.UserCollectionDao;
import com.dream.service.user.domain.entity.IsFollowCollect;
import com.dream.service.user.domain.entity.UserCollection;
import com.dream.service.user.domain.model.UserCollectionModel;
import com.dream.service.wrapper.api.ProductWrapperService;
import com.dream.service.wrapper.domain.entity.SPUData;
import com.dream.service.wrapper.domain.request.SPUDataRequest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by kangd on 2016/5/4.
 */
public class UserCollectionServiceImpl implements UserCollectionService{

    private UserCollectionDao userCollectionDao;

    public UserCollectionDao getUserCollectionDao() {
        return userCollectionDao;
    }

    @Autowired
    public void setUserCollectionDao(UserCollectionDao userCollectionDao) {
        this.userCollectionDao = userCollectionDao;
    }

    //service

    ProductWrapperService productWrapperService;

    public ProductWrapperService getProductWrapperService() {
        return productWrapperService;
    }

    public void setProductWrapperService(ProductWrapperService productWrapperService) {
        this.productWrapperService = productWrapperService;
    }


    /**
     * 增加收藏
     *
     * @param pin
     * @param spuList
     * @return
     */
    @Override
    public BaseResponse<Boolean> addWishList(String pin, List<Long> spuList) {
        long startTime = System.currentTimeMillis();
        if(spuList == null || spuList.size()<=0 || StringUtils.isEmpty(pin)){
            BaseResponse<Boolean> baseResponse= new BaseResponse<Boolean>(ErrorCode.PARAM_ERROR);
            return baseResponse;
        }
        BaseResponse<Boolean> baseResponse= new BaseResponse<Boolean>();
        try {
            List<Long>  wishList  = userCollectionDao.selectByPINCollection(pin,spuList);
            for(Long l :wishList){
                spuList.remove(l);
            }
            if(spuList.size()>0){
                int num = userCollectionDao.addUserCollection(pin,spuList);
                if(num ==spuList.size()){
                    baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
                }else {
                    baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
                }
            }else {
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }
        baseResponse.setLatency(System.currentTimeMillis()-startTime);
        return baseResponse;
    }

    /**
     * 删除收藏
     *
     * @param pin
     * @param spuList
     * @return
     */
    @Override
    public BaseResponse<Boolean> delWishList(String pin, List<Long> spuList) {
        long startTime = System.currentTimeMillis();
        if(spuList == null || spuList.size()<=0 || StringUtils.isEmpty(pin)){
            BaseResponse<Boolean> baseResponse= new BaseResponse<Boolean>(ErrorCode.PARAM_ERROR);
            return baseResponse;
        }
        BaseResponse<Boolean> baseResponse= new BaseResponse<Boolean>();
        try {
            int num = userCollectionDao.delUserCollection(pin,spuList);
            if(num ==spuList.size()){
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
            }else {
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }
        baseResponse.setLatency(System.currentTimeMillis()-startTime);
        return baseResponse;
    }

    /**
     * 拉取wishlist
     *
     * @param pin
     * @param pageNum  页码
     * @param pageSize 页面容量
     * @return
     */
    @Override
    public BaseListResponse getWishList(String pin, Integer pageNum, Integer pageSize) {
        long startTime = System.currentTimeMillis();
        if(StringUtils.isEmpty(pin)){
            BaseListResponse baseListResponse= new BaseListResponse(ErrorCode.PARAM_ERROR);
            return baseListResponse;
        }
        BaseListResponse baseListResponse= new BaseListResponse();
        ListModel listModel = new ListModel();
        List<UserCollection> userCollectionList=new ArrayList<UserCollection>();
        try {
            int orderAmout = userCollectionDao.wishListCountByPIN(pin);
            if(orderAmout == 0){
                listModel.setAmount(orderAmout);
                baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
                return baseListResponse;
            }else {
                listModel.setAmount(orderAmout);
                List<UserCollectionModel> userCollectionModels = null;
                if (pageNum == null || pageSize == null) {
                    userCollectionModels = userCollectionDao.selectByPIN(pin, null, null);
                } else if(pageNum>0 && pageSize>=0) {
                    userCollectionModels = userCollectionDao.selectByPIN(pin, (pageNum - 1) * pageSize, pageSize);
                }
                if (userCollectionModels == null || userCollectionModels.isEmpty()) {
                    listModel.setList(userCollectionList);
                    baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
                    return baseListResponse;
                }
                Set<Long> spuSet =new HashSet<Long>();
                for(UserCollectionModel userCollectionModel:userCollectionModels){
                    spuSet.add(userCollectionModel.getSpu());
                }
                SPUDataRequest spuDataRequest = new SPUDataRequest();
                spuDataRequest.setSpus(spuSet);
                spuDataRequest.setReturnPrice(true);
                spuDataRequest.setReturnStockStatus(true);
                BaseResponse<Map<Long, SPUData>> baseResponse= productWrapperService.getSPUData(spuDataRequest);
                if(baseResponse.isSuccess()){
                    Map<Long, SPUData> spuDataMap = baseResponse.getResult();
                    if(spuDataMap != null && spuDataMap.size()>0){
                        //以spu有返回结果为准，没有返回的不会显示到wishlist中
                        for(UserCollectionModel userCollectionModel:userCollectionModels){
                            SPUData spuData = spuDataMap.get(userCollectionModel.getSpu());
                            if(spuData != null){
                                SpuBase spuBase = spuData.getSpuBase();
                                if(spuBase == null){
                                    continue;
                                }
                                UserCollection userCollection = new UserCollection();
                                userCollection.setSpu(userCollectionModel.getSpu());
                                userCollection.setSkuPrice(spuData.getSkuPrice());
                                userCollection.setMain_sku(spuBase.getMain_sku());
                                userCollection.setMain_title(spuBase.getMain_title());
                                userCollection.setMain_image_url(spuBase.getMain_image_url());
                                userCollection.setSale_type(spuBase.getSale_type());
                                userCollection.setStatus(spuBase.getStatus_code());
                                userCollection.setStockStatus(spuData.getStockStatus().getId());
                                userCollection.setIsPutOn(spuBase.getIsPutOn());
                                userCollectionList.add(userCollection);
                            }
                        }
                        listModel.setList(userCollectionList);
                        baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
                    }else {
                        baseListResponse.packData(false, ErrorCode.PROCESS_ERROR.getCode(),"Product Service Error", null, null);
                    }
                }else {
                    baseListResponse.packData(false, ErrorCode.PROCESS_ERROR.getCode(),"Product Service Error", null, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseListResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }

        baseListResponse.setLatency(System.currentTimeMillis()-startTime);
        return baseListResponse;
    }

    /**
     * 是否收藏
     *
     * @param pin
     * @param spu
     * @return
     */
    @Override
    public BaseResponse<IsFollowCollect> iscollect(String pin, Long spu) {
        long startTime = System.currentTimeMillis();
        if(spu == null || StringUtils.isEmpty(pin)){
            BaseResponse<IsFollowCollect> baseResponse= new BaseResponse<IsFollowCollect>(ErrorCode.PARAM_ERROR);
            return baseResponse;
        }
        BaseResponse<IsFollowCollect> baseResponse = new BaseResponse<IsFollowCollect>();
        try {
            List<Long>  wishList  = userCollectionDao.selectByPINCollection(pin, Arrays.asList(spu));
            if(wishList!=null && wishList.size()==1){
                IsFollowCollect isFollowCollect = new IsFollowCollect();
                isFollowCollect.setIsFC(true);
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, isFollowCollect);
            }else {
                IsFollowCollect isFollowCollect = new IsFollowCollect();
                isFollowCollect.setIsFC(false);
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, isFollowCollect);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }
        baseResponse.setLatency(System.currentTimeMillis() - startTime);
        return baseResponse;
    }

    public BaseListResponse getWishListOnlySpu(String pin){
        long startTime = System.currentTimeMillis();
        if(StringUtils.isEmpty(pin)){
            BaseListResponse baseListResponse= new BaseListResponse(ErrorCode.PARAM_ERROR);
            return baseListResponse;
        }
        BaseListResponse baseListResponse= new BaseListResponse();
        try {
            int defaultLimit = 500;
            List<Long> spuList = userCollectionDao.selectSpusByPIN(pin, defaultLimit);
            ListModel listModel = new ListModel();
            listModel.setAmount(spuList == null ? 0 : spuList.size());
            listModel.setList(spuList);
            baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
        } catch (Exception e) {
            e.printStackTrace();
            baseListResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }

        baseListResponse.setLatency(System.currentTimeMillis()-startTime);
        return baseListResponse;
    }
}
